package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkInfoAnalyseShortQuesService;

/**
 * 简单题分析
 */
@CrossOrigin
@RestController
public class HomeworkShortQuesController extends YxyApiBaseController {

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    HomeworkInfoAnalyseShortQuesService homeworkInfoAnalyseShortQuesService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workinfo/analyse/shortques", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            ReqModel reqModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        reqModel.taskId = taskId;
        reqModel.classId = classId;
        // ##
        // 任务试题列表 QuesType: 0--单选择题，1--多选择题，2--通知类
        List<HomeworkTaskQues> homeworkTaskQueslist = homeworkTaskQuesRepository.findByQuesTypeAndTaskId("4", taskId);
        // 学生提交单选题数量
        List<HomeworkTaskReportQues> homeworkTaskReportQueslist = homeworkTaskReportQuesRepository
                .findByTaskIdAndQuestionTypeAndClassId(taskId, "4", classId);
        // 数据转化模型
        return homeworkInfoAnalyseShortQuesService.convertRespModel(homeworkTaskQueslist, homeworkTaskReportQueslist,
                taskId, classId);
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 用户Id (不用传参)
        public String userId;
        // 学校Id (不用传参)
        public String schoolId;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // 任务中题的序号
        public Integer order;
        // 试题:ID， 如果是通知类作业：tz+随机数
        public String quesId;
        // 正确率
        public Double accuracy;
        // 题干
        public Object quesBody;
        // 应得分数
        public Integer dueTotal;
        // 实际分数
        public Integer factTotal;
        // 选择人数（人）
        public List<SelectPeople> selectPeoples;
        // 学生名单
        public List<List<String>> students;
    }

    public static class SelectPeople {
        // 分数
        public Integer mark;
        // 人数
        public int total;
    }
}
